package com.vgooo.client.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.umeng.analytics.MobclickAgent;
import com.vgooo.bean.Brand;
import com.vgooo.bean.ProdAbstract;
import com.vgooo.client.Main;
import com.vgooo.client.R;
import com.vgooo.client.ViewLayout;
import com.vgooo.client.mobile.MobileAdapter;
import com.vgooo.client.mobile.MobileMainFragmentActivity;
import com.vgooo.util.BitmapUtils;
import com.vgooo.util.DensityUtil;
import com.vgooo.util.Utils;

public final class MobileFragment extends Fragment {
    private static final String KEY_CONTENT = "MobileFragment:Content";
	
	private Brand brand;	
	private MobileAdapter adapter;
	private List<ProdAbstract> prodsInfo;// 热门推荐手机列表
	private MobileMainFragmentActivity context;
	private View view;
	
	private static Map<String, Map<String, Object>> fragmentDatas = new HashMap<String, Map<String, Object>>();
	private static Map<String, Object> fragmentCache = new HashMap<String, Object>();
	
	public static final MobileFragment newInstance(Brand brand, List<ProdAbstract> prodsInfo, MobileMainFragmentActivity context)
	{
//		log("newInstance()  " + brand.brand_name) ;
		if (fragmentCache.get(brand.brand_name)==null)
		{
			HashMap<String, Object> fragmentItem = new HashMap<String, Object>();
			fragmentItem.put("brand", brand);
			fragmentItem.put("prodsInfo", prodsInfo);
			fragmentItem.put("context", context);
			
			fragmentDatas.put(brand.brand_name, fragmentItem);
			
			
			MobileFragment fragment = new MobileFragment(brand, prodsInfo, context);
			Bundle bundle = new Bundle();
			bundle.putString(KEY_CONTENT, brand.brand_name);
			fragment.setArguments(bundle);
			
			fragmentCache.put(brand.brand_name, fragment); 
		}
		
		if(MobileMainFragmentActivity.HOT_PROD.equals(brand.brand_name)&&prodsInfo!=null&&!prodsInfo.isEmpty()){
			MobileAdapter.prods.put(brand.brand_name, prodsInfo);
		}
		return (MobileFragment)fragmentCache.get(brand.brand_name);
		
	}	
	
	public MobileFragment(Brand brand, List<ProdAbstract> prodsInfo, MobileMainFragmentActivity context) {
		this.brand = brand;
		this.prodsInfo = prodsInfo;
		this.context = context;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		if (getArguments() != null) 
		{
			String brand_name = getArguments().getString(KEY_CONTENT);
			
			HashMap<String, Object> map = (HashMap<String, Object>) fragmentDatas.get(brand_name);
			this.brand = (Brand)map.get("brand");
			this.prodsInfo = (List<ProdAbstract>)map.get("prodsInfo");
			this.context = (MobileMainFragmentActivity)map.get("context");
		}
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(getActivity());
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(getActivity());
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putString(KEY_CONTENT, brand.brand_name);
	}
	
	
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
	}

	public MobileFragment() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
//		log("onCreateView()  " + brand.brand_name) ;
		view = inflater.inflate(R.layout.mobile_fragment, null);		
		adapter = new MobileAdapter(prodsInfo, context);
		
		try {
			if (!MobileMainFragmentActivity.HOT_PROD.equals(brand.brand_name))
			{
				prodsInfo = MobileAdapter.prods.get(brand.brand_name);
				if(prodsInfo==null||prodsInfo.isEmpty()){
					adapter.dataChanage(brand);
				}else{
					adapter.dataChanage(prodsInfo);
				}
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		GridView mobileGView = (GridView) view.findViewById(R.id.mobile_main_gridView);
		mobileGView.setOnScrollListener(BitmapUtils.onScrollListener);
		mobileGView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// TODO Auto-generated method stub
				if(Utils.netWorkInfoToast(getActivity())){
					return ;
				}
				ProdAbstract prod = (ProdAbstract) adapter.getItem(position);
				if (prod.isBlank) {
					return;
				}

				StringBuffer link = new StringBuffer("proddetail?");
				link.append("b=").append(prod.brand_html).append("&p=")
						.append(prod.prod_html).append("&sw=")
						.append("" + DensityUtil.dip2px(context, 320));
				
				Main.moblieDetailIntent.putExtra("link", link.toString());
				ViewLayout.setViewPre(true, "手机详情",
						ViewLayout.TAB_TAG_MOBILE_DETAIL, context.cparent);
			}
		});
		mobileGView.setAdapter(adapter);
		return view;
	}	
	
	
	
	public static void clearCaches(){

		if(fragmentCache!=null&&!fragmentCache.isEmpty()){
			fragmentCache.clear() ;
		}
		MobileAdapter.clearCaches();
		System.gc();
	}
	

	
	private static void log(String info){
		Log.i("MobileFragment", info) ;
	}
	
}
